package com.chaochis.wind.controller;

import com.chaochis.wind.base_config.page_config.NeedPageHelper;
import com.chaochis.wind.base_config.page_config.api.ApiResult;
import com.chaochis.wind.params.wind_user_model.ReqWindUserListQuery;
import com.chaochis.wind.service.WindUserService;
import com.chaochis.wind.vo.WindAccount;
import com.chaochis.wind.vo.WindUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/wind_user")
public class WindUserController {
  private WindUserService windUserService;
  
  @Autowired
  public void setWindUserService(WindUserService windUserService) {
    this.windUserService = windUserService;
  }
  
  /**
   * 获取系统的用户列表
   *
   * @param windUserListQueryModel 查询参数
   * @return 返回查询到的用户列表
   */
  @GetMapping("/get_list")
  @NeedPageHelper
  public ApiResult<List<WindUser>> getWindUserList(ReqWindUserListQuery windUserListQueryModel) {
    System.out.println(windUserListQueryModel);
    var windUser = new WindUser();
    BeanUtils.copyProperties(windUserListQueryModel, windUser);
    return ApiResult.success(this.windUserService.getWindUserList(windUser));
  }
  
  /**
   * 获取某个用户绑定的账户
   * @param id 用户id
   * @return 返回该用户绑定的所有账户
   */
  @GetMapping("/get_bound_accounts")
  public ApiResult<List<WindAccount>> findAccountListByUserId(String id) {
    return ApiResult.success(this.windUserService.getAccountsFromUserId(id));
  }
  
   /**
   * 删除某个用户（合并了逻辑删除与物理删除）
   * @param id 用户id
   * @return 成功结果
   */
  @PostMapping("/delete_user")
  public ApiResult<Boolean> deleteWindUser(@RequestBody String id) throws Exception {
    this.windUserService.deleteWindUser(id);
    return ApiResult.success(true);
  }
  
  /**
   * 激活用户
   * @param id 激活用户
   * @return 成功结果
   */
  @GetMapping("/active_user")
  public ApiResult<Boolean> activeOrReactiveWindUser(String id) throws Exception {
    this.windUserService.activeWindUser(id);
    return ApiResult.success(true);
  }
  
  /**
   * 更新用户信息
   * @param windUser 要更新的windUser用户信息
   * @return 返回更新成功
   * @throws Exception 抛出异常
   */
  @PostMapping("/update_user")
  public ApiResult<Boolean> addWindUser(@RequestBody WindUser windUser) throws Exception{
    var updateWindUser = new WindUser();
    BeanUtils.copyProperties(windUser, updateWindUser);
    this.windUserService.updateWindUserInfo(updateWindUser);
    return ApiResult.success(true);
  }
}
